#include "selfBoneManager.h"

selfBoneManager* selfBoneManager::_instance = 0;

selfBoneManager* selfBoneManager::Instance()
{
	if (_instance == NULL)
	{
		_instance = new selfBoneManager();
	}
	return _instance;
}

selfBoneManager::selfBoneManager()
{
	for (int i = 0; i < 1000; i++)
	{
		freeMem.push_back(new Bullet());
	}
	_num = 0;
}

void selfBoneManager::draw()
{
	for (list<Bullet*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		(*itor)->draw();
	}
}

/*
void selfBoneManager::add(Bullet* b)
{
	manager.push_front(b);
}
*/

void selfBoneManager::add(stdBullet lib, Point p, Point* p1, int i)
{
	if (freeMem.empty())
	{
		cout << "the freeMem is null" << endl;
		return;
	}
	list<Bullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(lib, p, p1, i);
	manager.splice(manager.begin(), freeMem, itor);
}

void selfBoneManager::updata()
{
	//cout << freeMem.size() << " " << manager.size() << endl;
	for (list<Bullet*>::iterator itor = manager.begin();
		itor != manager.end();)
	{
		if ((*itor)->checkRemove())
		{
			/*delete *itor;
			itor = manager.erase(itor);*/
			list<Bullet*>::iterator itor1 = itor;
			itor1++;
			freeMem.splice(freeMem.begin(), manager, itor);
			itor = itor1;
		}else
		{
			(*itor)->updata();
			itor++;
		}
	}
}

void selfBoneManager::clear()
{
	freeMem.splice(freeMem.begin(), manager);
}